package com.huluobo.a2210mdmdemo.video

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.media3.common.MediaItem
import androidx.media3.common.Player.REPEAT_MODE_ONE
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView
import androidx.navigation.NavHostController
import com.google.gson.Gson
import com.huluobo.a2210mdmdemo.entity.VideoEntityItem
import com.huluobo.lib_http.Const
import com.tencent.mmkv.MMKV

/**
 *  Created by LC on 2025/4/6.
 */
@Composable
fun VideoDetailPage() {
    val context = LocalContext.current
    val videoEntityJson = MMKV.defaultMMKV().decodeString(Const.PARAM_VIDEO_KEY)
    val videoEntity = Gson().fromJson(videoEntityJson, VideoEntityItem::class.java)

    val exoPlayer = remember {
        ExoPlayer.Builder(context).build().apply {
            setMediaItem(MediaItem.fromUri(videoEntity.videopath))
            prepare()
            playWhenReady = true
            repeatMode = REPEAT_MODE_ONE
        }
    }
    Column(
        modifier = Modifier
            .padding(10.dp)
            .fillMaxSize()
    ) {
        AndroidView(factory = {
            PlayerView(context).apply {
                player = exoPlayer
                useController = true
            }
        })
    }

    DisposableEffect(Unit) {
        onDispose {
            exoPlayer.release()
        }
    }
}